package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiU_link_group;

import cn.com.api.service.ApiU_link_groupService;

import cn.com.util.*;
import cn.com.util.*;

public class U_link_groupBaseController {

	private static Logger logger = Logger.getLogger(U_link_groupBaseController.class);

	@Autowired
	private ApiU_link_groupService apiU_link_groupService;

	/**
	 * 列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_link_group-list")
	public ModelAndView u_link_group_list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/u_link_group_list.jsp");

		List<ApiU_link_group> u_link_groups = apiU_link_groupService.findU_link_groups();
		mv.addObject("u_link_groups", u_link_groups);

		return mv;
	}

	/**
	 * 进入新增或者编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_link_group-add-pre")
	public ModelAndView u_link_group_add_pre(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/u_link_group_add.jsp");
		//
		if (request.getParameter("id") != null) {
			String id = request.getParameter("id");
			ApiU_link_group u_link_group = apiU_link_groupService.findU_link_groupById(Integer.valueOf(id));
			mv.addObject("u_link_group", u_link_group);
		}
		return mv;
	}

	/**
	 * 新增或者编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_link_group-add")
	public void u_link_group_add(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String sort = request.getParameter("sort");
		String is_effect = request.getParameter("is_effect");

		Map<String, Object> map = new HashMap<String, Object>();

		if (id == null || (id.trim()).equals("")) {// add
			ApiU_link_group u_link_group = new ApiU_link_group();
			u_link_group.setName(name);
			u_link_group.setSort(Integer.valueOf(sort));
			u_link_group.setIs_effect(Integer.valueOf(is_effect));
			apiU_link_groupService.insertU_link_group(u_link_group);
		} else {// edit
			ApiU_link_group u_link_group = apiU_link_groupService.findU_link_groupById(Integer.valueOf(id));
			u_link_group.setName(name);
			u_link_group.setSort(Integer.valueOf(sort));
			u_link_group.setIs_effect(Integer.valueOf(is_effect));
			apiU_link_groupService.updateU_link_group(u_link_group);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_link_group-delete")
	public void u_link_group_delete(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");

		// delete
		int ret_i = apiU_link_groupService.deleteU_link_group(Integer.valueOf(id));

		//
		if (ret_i > 0) {
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

}
